//
//  UIManager.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/7/2.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "UIManager.h"

#import "MHNavigationController.h"
#import "LoadingViewController.h"
#import "EPWebViewController.h"

#import <IQKeyboardManager/IQKeyboardManager.h>

@implementation UIManager

MHSingletonM(UIManager);

- (id)init
{
    self = [super init];
    
    if (self)
    {
        [self initData];
        [self initUI];
    }
    
    return self;
}

- (void)initData
{
    [[[[NSNotificationCenter defaultCenter] rac_addObserverForName:Notify_Article_Push object:nil] deliverOnMainThread] subscribeNext:^(NSNotification* x)
    {
        NSDictionary* pushData = x.object;
        
        if ([[AppDelegate sharedDelegate].window.rootViewController isKindOfClass:[LoadingViewController class]])
        {
            [[DataManager sharedDataManager] savePushData:pushData];
        }
        else
        {
            UITabBarController* tabbarCtrl = (UITabBarController*) [AppDelegate sharedDelegate].window.rootViewController;
            
            UINavigationController* curNavc = tabbarCtrl.selectedViewController;
        
            NSString* strPushType = pushData[@"type"];
            
            if ([strPushType isEqualToString:@"Article"])
            {
                ArticleData* aData = [[ArticleData alloc] init];
                aData.articleID = [pushData[@"ArticleId"] integerValue];
                aData.title = pushData[@"Title"];
                aData.url = pushData[@"url"];
                
                EPWebViewModel* epWebVM = [[EPWebViewModel alloc] initWithParams:@{SUViewModelTitleKey:aData.title}];
                epWebVM.articleData = aData;
                
                EPWebViewController* vc = [[EPWebViewController alloc] initWithViewModel:epWebVM];
                
                MHNavigationController* newNavc = [[MHNavigationController alloc] initWithRootViewController:vc];
                
                [curNavc presentViewController:newNavc animated:YES completion:^{
                    
                }];
            }
        }
    }];
}

- (void)initUI
{
    //全局配置UI属性
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [[IQKeyboardManager sharedManager] setToolbarTintColor:[UIColor blackColor]];
    [[IQKeyboardManager sharedManager] registerTextFieldViewClass:[YYTextView class] didBeginEditingNotificationName:YYTextViewTextDidBeginEditingNotification didEndEditingNotificationName:YYTextViewTextDidEndEditingNotification];
}

@end
